<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_shop1miao/model')->方法名();
*/
namespace jy_shop1miao;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function up_zu($zu,$zukey=0){
		$zu['key']=$zukey;
		$zu['title']=$zu['num'].$this->zuday_types[$zu['numtype']];
		$zu['unit']=$this->zuday_types[$zu['numtype']];
		$zu['price']=price($zu['price'],'int');
		$zu['starttime']=date('Y-m-d',time()+2*24*3600);
		if($zu['numtype']==0){
			if($zu['num']>30){
				$zu['allqi']=intval($zu['num']/30);
				$num=30;
			}else{
				$zu['allqi']=1;
				$num=$zu['num'];
			}
		}elseif($zu['numtype']==1){
			$num=$zu['num']*30;
			$zu['allqi']=intval(365/($zu['num']*30));
		}elseif($zu['numtype']==2){
			$num=$zu['num']*365;
			$zu['allqi']=1;
		}
		$zu['total_price']=price($num*$zu['price'],'int');
		$enddays=2+$zu['num'];
		$zu['endtime']=date('Y-m-d',time()+$enddays*24*3600);
		$zu['lastqi']=$zu['allqi']-1;
		$zu['allprice']=price($zu['allqi']*$zu['total_price'],'int');
		return $zu;
	}
	public function up_zu2($zu,$zukey=0){
		$zu['key']=$zukey;
		$zu['title']=$zu['num'].$this->zuday_types[$zu['numtype']];
		$zu['unit']=$this->zuday_types[$zu['numtype']];
		$zu['total_price']=price($zu['num']*$zu['price'],'int');
		$zu['price']=price($zu['price'],'int');
		$zu['starttime']=date('Y-m-d',time()+2*24*3600);
		if($zu['numtype']==0){
			$num=$zu['num'];
			$zu['allqi']=intval(365/$zu['num']);
		}elseif($zu['numtype']==1){
			$num=$zu['num']*30;
			$zu['allqi']=intval(365/($zu['num']*30));
		}elseif($zu['numtype']==2){
			$num=$zu['num']*365;
			$zu['allqi']=1;
		}
		$enddays=2+$num;
		$zu['endtime']=date('Y-m-d',time()+$enddays*24*3600);
		$zu['lastqi']=$zu['allqi']-1;
		$zu['allprice']=price($zu['allqi']*$zu['total_price'],'int');
		return $zu;
	}
	public function uplog($order,$qinum=0,$nopay=false,$ispay=0){
		$log=$this->pdo_get('log',array('orderid'=>$order['id'],'qinum'=>$qinum));
		if(!$log['id']){
			$price=$order['api_data']['total_price'];
			$log=array(
				'uniacid'=>$_SESSION['uniacid'],
				'orderid'=>$order['id'],
				'mid'=>$_SESSION['uid'],
				'price'=>$price,
				'payfee'=>$price,
				'qinum'=>$qinum,
				'ispay'=>$ispay,
				'status'=>$ispay,
				'createtime'=>TIMESTAMP
			);
			$this->pdo_insert('log',$log);
			$log['id']=pdo_insertid();
			if(!$nopay){
				$params=array(
					'id'=>$log['id'],
					'title'=>$qinum.'期续租',
					'fee'=>$price,
					'back_url'=>'jy_shop1miao/order.zudan/payresult',
					'success_url'=>'jy_shop1miao/order.list/',
					'error_url'=>'jy_shop1miao/order.list/',
				);
				$payid=cfc('pay')->create_pay_order($params);
				$this->pdo_update('log',array('pay_id'=>$payid),array('id'=>$log['id']));
				$log['pay_id']=$payid;
			}
		}
		return $log;
	}
	//租赁中
	public function order_receive($orderid=0){
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		if($order['api_data'] && $order['api_pluginname']=='jy_shop1miao'){
			$zu=json_decode($order['api_data'],true);
			$zu['starttime']=date('Y-m-d');
			if($zu['numtype']==0){
				$num=$zu['num'];
			}elseif($zu['numtype']==1){
				$num=$zu['num']*30;
			}elseif($zu['numtype']==2){
				$num=$zu['num']*365;
			}
			$zu['endtime']=date('Y-m-d',time()+$num*24*3600);
			$zu['status']=2;
			//租赁中
			pdo_update('jy_weishop_order',array('api_data'=>ijson_encode($zu)),array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		}
		model('jy_weishop/order')->order_receive($orderid);
		if($num){
			pdo_update('jy_weishop_order',array('status'=>22),array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		}
		return true;
	}
	//续租赁中
	public function order_xuzu($orderid=0){
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		if($order['api_data'] && $order['api_pluginname']=='jy_shop1miao'){
			$zu=json_decode($order['api_data'],true);
			$endtime=strtotime($zu['endtime'])+30*24*3600;
			$zu['endtime']=date('Y-m-d',$endtime);
			//租赁中
			pdo_update('jy_weishop_order',array('api_data'=>ijson_encode($zu)),array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		}
		return true;
	}
	//发起扣款
	public function xinyong_pay($logid=0,$orderid=0,$qinum=0,$noexi=0){
		if($logid){
			$log=$this->pdo_get('log',array('uniacid'=>$_SESSION['uniacid'],'id'=>$logid));
		}else{
			$log=$this->pdo_get('log',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$orderid,'qinum'=>$qinum));
		}
		if($log['ispay']){
			return '';
		}
		if(!$log['id']){
			$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
			$order['api_data']=json_decode($order['api_data'],true);
			$price=$order['api_data']['total_price'];
			$log=$this->model('model')->uplog($order,$qinum);
		}else{
			$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['orderid']));
		}
		$pay_log=pdo_get('core_paylog',array('id'=>$log['pay_id'],'uniacid'=>$_SESSION['uniacid']));
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$order['wxapp_id']?:$_SESSION['aliapp_id'],'uniacid'=>$_SESSION['uniacid']));
		$wxapp['api_data']=json_decode($wxapp['api_data'],true);
		$pay_log['ali_service_id']=$wxapp['api_data']['ali_service_id'];
		if(!$pay_log['ali_service_id']){
			if($noexi){
				return '';
			}else{
				exi('小程序未绑定信用服务ID！','error');
			}
		}
		$setting=array(
			'alipay_appid'=>$wxapp['ali_appid'],
			'alipay_private_key'=>$wxapp['alipay_private_key'],
			'alipay_public_key'=>$wxapp['alipay_public_key'],
			'target_app_id'=>$wxapp['target_app_id'],
		);
		model('jy_alipay/alipay',$setting)->xinyong_pay($pay_log);
		return true;
	}
	//取消
	public function cancel_xinyong($orderid=0){
		$order=pdo_get('jy_weishop_order',array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']));
		$api_data=json_decode($order['api_data'],true);
		$pay_log=pdo_get('core_paylog',array('id'=>$order['pay_id'],'uniacid'=>$_SESSION['uniacid']));
		if($pay_log['oldfee']>0 && $api_data['is_xinyong'] && !$api_data['cancel_xinyong']){
			$params=array(
				'title'=>$pay_log['title'],
				'fee'=>$pay_log['fee'],
				'ordersn'=>$pay_log['tid'],
				'success_url'=>$pay_log['success_url'],
				'error_url'=>$pay_log['error_url'],
			);
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$order['wxapp_id']?:$_SESSION['aliapp_id'],'uniacid'=>$_SESSION['uniacid']));
			$wxapp['api_data']=json_decode($wxapp['api_data'],true);
			$params['ali_service_id']=$wxapp['api_data']['ali_service_id'];
			if(!$params['ali_service_id']){
				exi('小程序未绑定信用服务ID！','error');
			}
			$params['yafee']=$pay_log['oldfee'];
			$setting=array(
				'alipay_appid'=>$wxapp['ali_appid'],
				'alipay_private_key'=>$wxapp['alipay_private_key'],
				'alipay_public_key'=>$wxapp['alipay_public_key'],
				'target_app_id'=>$wxapp['target_app_id'],
			);
			model('jy_alipay/alipay',$setting)->aliapp_cancel_xinyong($params);
			$api_data['cancel_xinyong']=1;
			pdo_update('jy_weishop_order',array('api_data'=>ijson_encode($api_data)),array('id'=>$orderid,'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	public function checkshiming(){
		if(!$_SESSION['uid']){
			return 0;
		}
		$member1=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('cardnum','mobile','realname'));
		$member=pdo_get('jy_shop1miao_member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('id','status'));
		$memberid=$member['id'];
		$basic=$this->plugin_setting('basic');
		if(($basic['is_shiming'] || $basic['is_esgin']) && (!$memberid || !$member1['cardnum'] || !$member1['realname'] || !$member1['mobile'])){
			return 1;
		}
		if($basic['is_shiming'] && !$member['status']){
			return 1;
		}
		return 0;
	}
}
?>